package com.example.lianguisystem.service.Impl;


import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.lianguisystem.entity.CommonSetting;
import com.example.lianguisystem.mapper.CommonSettingMapper;
import com.example.lianguisystem.service.CommonSettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CommonSettingServiceImpl extends ServiceImpl<CommonSettingMapper, CommonSetting> implements CommonSettingService {

    @Autowired
    private CommonSettingMapper commonSettingMapper;

    @Override
    public CommonSetting getCommonSetting(String username, String type) {
        CommonSetting commonSetting = commonSettingMapper.selectCommonSetting(username, type);
        if (commonSetting == null) {
            commonSetting = new CommonSetting();
            commonSetting.setUsername(username);
            commonSetting.setItemType(type);
        }
        return commonSetting;
    }

    @Override
    public Boolean addOrEditCommonSetting(CommonSetting commonSetting) {
        CommonSetting temp = commonSettingMapper.selectCommonSetting(commonSetting.getUsername(), commonSetting.getItemType());
        Integer isSuccess = 0;
        if (temp == null) {
            isSuccess = commonSettingMapper.insert(commonSetting);
        } else {
            temp.setA4Printer(commonSetting.getA4Printer());
            temp.setLabelPrinter(commonSetting.getLabelPrinter());
            temp.setExtraField(commonSetting.getExtraField());
            isSuccess = commonSettingMapper.updateById(temp);
        }
        return isSuccess > 0;
    }

}
